package com.qf.smsplatform.monitor.config;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.qf.smsplatform.monitor.job.MonitorRabbitJob;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MonitorRabbitJobConfig {

    // TODO 写死, 从配置文件中取
    private String zookeeperList = "192.168.199.109:2181";

    @Bean
    public CoordinatorRegistryCenter registryCenter(){
        CoordinatorRegistryCenter registryCenter =
                new ZookeeperRegistryCenter(
                        new ZookeeperConfiguration(zookeeperList,"smsPlatformMonitor"));

        registryCenter.init();

        return registryCenter;
    }


    @Bean
    public SpringJobScheduler jobScheduler(MonitorRabbitJob monitorRabbitJob,CoordinatorRegistryCenter registryCenter){
        // 定义作业核心配置
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("monitorRabbitQueue", "0/5 * * * * ?", 1).build();
        // 定义SIMPLE类型配置
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, MonitorRabbitJob.class.getCanonicalName());
        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();
        // 定义SpringJobScheduler类执行任务
        SpringJobScheduler jobScheduler = new SpringJobScheduler(monitorRabbitJob,registryCenter,simpleJobRootConfig);
        // 初始化任务
        jobScheduler.init();
        // 返回
        return jobScheduler;
    }



}
